﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chess
{
    public class Board
    {
        static Square[] arraySquare = new Square[128];

        static Board()
        {
            for(int ordinal=0; ordinal<128; ordinal++)
                arraySquare[ordinal] = new Square(ordinal);
        }

        public static Square GetSquare(int ordinal)
        {
            return(ordinal & 0x88) == 0 ? arraySquare[ordinal]:null;
        }

        public static Square GetSquare(int file,int rank)
        {
            return((rank << 4 | file) & 0x88) == 0 ? arraySquare[(rank << 4 | file)] : null;
        }

        public static Piece GetPiece(int ordinal)
        {
            return (ordinal & 0x88) == 0 ? arraySquare[ordinal].piece : null;
        }

        public static Piece GetPiece(int file,int rank)
        {
            return ((rank << 4 | file) & 0x88) == 0 ? arraySquare[(rank << 4 | file)].piece : null;
        }

        public static void PieceLine(Moves moves,Piece piece,Player player,int offset)
        {
            int ordinal = piece.Square.Ordinal;
            Square square;

            ordinal += offset;
            while ((square = Board.GetSquare(ordinal)) != null)
            {
                if (square.Piece == null)       
                    moves.Add(piece, piece.Square, square, null);                  
                else
                    if (square.Piece.Player.isColor != player.isColor)
                    {
                        moves.Add(piece, piece.Square, square, square.Piece);
                        break;
                    }
                    else
                        break;
               
                ordinal += offset;
            }				
        }

    }
}
